<?php if (!defined('THINK_PATH')) exit();?><div class="bjui-pageHeader">
    <div class="pull-right">
        <button class="btn-default" data-icon="refresh" onclick="$.CurrentNavtab.navtab('refresh')"></button>
    </div>
</div>
<div class="bjui-pageContent">
    <form action="<?php echo U('save_model');?>" data-toggle="validate" data-alertmsg="false">
        <input type="hidden" name="mid" value="<?php echo ($va["id"]); ?>"/>
        <div class="form-group">
            <label class="x100">模型名称:</label>
            <input type="text" name="name" data-rule="required" value="<?php echo ($va["name"]); ?>"/>
        </div>
        <div class="form-group">
            <label class="x100">规格项:</label>
            <input type="hidden" name="spec_name" class="types_call" placeholder="不要更改这个input, 只是拿来触发事件的" />
            <button type="button" class="btn-green" data-toggle="lookupbtn" data-title="选择规格" data-id="sel_types" data-width="800" data-height="500" data-url="<?php echo U('Goods/spec', array('type'=>2));?>">添加</button>
        </div>
        <div class="form-group spec">
            <table class="table table-bordered table-hover spec_table" data-selected-multi="false" <?php if($va['spec'] == null): ?>style="display: none;"<?php endif; ?>>
                <thead>
                <tr>
                    <th width="200">规格名称</th>
                    <th>规格项</th>
                    <th>权重</th>
                    <th width="70">操作</th>
                </tr>
                </thead>
                <tbody>
                <?php if($va['spec'] != null): $countspec = count($va['spec']); ?>
	                <?php if(is_array($va['spec'])): $i = 0; $__LIST__ = $va['spec'];if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$vaa): $mod = ($i % 2 );++$i;?><tr data-id="<?php echo ($vaa["id"]); ?>"><td><?php echo ($vaa["name"]); ?></td><td><?php echo ($vaa["value"]); ?></td><td><input type="hidden" name="spec_ids[]" value="<?php echo ($vaa["id"]); ?>"><input type="text" name="spec_sort[]" class="form-control" value="<?php echo ($countspec-$i); ?>"></td><td><button type="button" class="btn btn-red">删</button></td></tr><?php endforeach; endif; else: echo "" ;endif; endif; ?>
                </tbody>
            </table>
        </div>
        <div class="form-group">
            <label class="x100">属性参数:</label>
            <input type="hidden" name="attr_name" class="attr_call" placeholder="不要更改这个input, 只是拿来触发事件的" />
            <button type="button" class="btn-green" data-toggle="lookupbtn" data-title="选择属性" data-id="sel_attr" data-width="800" data-height="500" data-url="<?php echo U('Goods/attr', array('type'=>2));?>">添加</button>
        </div>
        <div class="form-group">
            <table class="table table-bordered table-hover attr_table" data-selected-multi="false" <?php if($va['attr'] == null): ?>style="display: none;"<?php endif; ?> >
                <thead>
                <tr>
                    <th>属性名称</th>
                    <th>输入方式</th>
                    <th>可选值</th>
                    <th>权重</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php if($va['attr'] != null): if(is_array($va['attr'])): $k = 0; $__LIST__ = $va['attr'];if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$vao): $mod = ($k % 2 );++$k; $countattr = count($va['attr']); switch($vao['type']){ case '1': $typename = "单选"; break; case '2': $typename = "多选"; break; case '3': $typename = "下拉"; break; case '4': $typename = "文本输入"; break; case '5': $typename = "多行文本输入"; break; }; ?>
                   <tr data-id="<?php echo ($vao["id"]); ?>"><td><?php echo ($vao["name"]); ?></td><td><?php echo ($typename); ?></td><td><?php echo ($vao["value"]); ?></td><td><input type="hidden" name="attr_ids[]" value="<?php echo ($vao["id"]); ?>"><input type="text" name="attr_sort[]" value="<?php echo ($countattr-$k); ?>"></td><td><button type="button" class="btn btn-red">删</button></td></tr><?php endforeach; endif; else: echo "" ;endif; endif; ?>
                </tbody>
            </table>
        </div>

    </form>
</div>
<script>
    $(function () {
        function compose(tab, data,funback) {
            var result = data.args;
            var arr = [];
            if( (result.hasOwnProperty('id') && (result.id + "").indexOf(',') < 1) ){
                arr.push(result);
            }else{
                for (var item in result){
                    var text = result[item].split(',');
                    if( arr.length < 1 ){
                        for( var index = 0; index < text.length; index++ ){
                            var o = {};
                            o[item] = text[index];
                            arr.push(o);
                        }
                    }else{
                        for(var index = 0 ; index < arr.length ; index++){
                            arr[index][item] = text[index];
                        }
                    }
                }
            }
            var goodsTableBody = $('tbody', tab),
                    otr = goodsTableBody.find('tr'),
                    proidArr = [];
            if( otr.length < 1 ) {
                tab.show();
            }
            otr.each(function (i, item) {
                proidArr.push(parseInt($(this).attr('data-id')));
            });
            var htmlString = '';
            for (var index = 0 ; index < arr.length; index++){
                if( proidArr.length > 0 && $.inArray(parseInt(arr[index].id), proidArr) != -1 ){
                    continue;
                }else{
//                     在tr上放着ID,去重
                    htmlString += '<tr data-id="'+arr[index].id+'">';
                    htmlString += funback.call(this, arr[index]);
                    htmlString += '</tr>';
                }
            }
            goodsTableBody.append(htmlString);
        }
//        规格
        $('.types_call').on('afterchange.bjui.lookup', function (e, result) {
            compose($('.spec_table'), result, function (ret) {
                var str = '<td>'+ret.spec_name+'</td><td>'+ ret.spec_item.split('_').join(',')+'</td>';
                    str += '<td><input type="hidden" name="spec_ids[]" value="'+ret.id+'" /><input type="text" name="spec_sort[]" class="form-control" value="0" /></td>';
                    str += '<td><button type="button" class="btn btn-red">删</button></td>';
                return str;
            });
        });
//        属性
        $('.attr_call').on('afterchange.bjui.lookup', function (e, result) {
            compose($('.attr_table'), result, function(ret){
                var str = '<td>'+ret.attr_name+'</td><td>'+ret.attr_input+'</td>';
                    str += '<td>'+(function(item){
                                if( item ){
                                    if( item.indexOf('_') > 0 ){
                                        return item.split('_').join(',');
                                    }
                                }
                                return item || "";
                            })(ret.attr_item)+'</td>';
                    str += '<td><input type="hidden" name="attr_ids[]" value="'+ret.id+'"/><input type="text" name="attr_sort[]" value="0" /></td>';
                    str += '<td><button type="button" class="btn btn-red">删</button></td>';
                return str;
            });
        });
        $('.attr_table,.spec_table').on('click', 'button.btn-red', function () {
            $(this).parents('tr').remove();
        });

    });
</script>

<div class="bjui-pageFooter">
		
	<?php if(save == save): ?><ul>
			<li><button type="submit" class="btn-default" data-icon="save">保存</button></li>
	        <li><button type="button" class="btn-close" data-icon="close">关闭</button></li>
	    </ul>
	<?php elseif(save == close): ?>
	    <ul>
	        <li><button type="button" class="btn-close" data-icon="close">关闭</button></li>
	    </ul>
	<?php elseif(save == page and $show != null): ?>
		<ul>
	        <li><button type="button" class="btn-close" data-icon="close">关闭</button></li>
	    </ul>
		    <div class="pages">
		        <span>每页&nbsp;</span>
		        <div class="selectPagesize">
		            <select data-toggle="selectpicker" data-toggle-change="changepagesize">
		                <option value="<?php echo ($show['listrow']); ?>"><?php echo ($show['listrow']); ?></option>
		                <option value="60">60</option>
		                <option value="120">120</option>
		                <option value="150">150</option>
		            </select>
		        </div>
		        <span>&nbsp;条，共 <?php echo ($show['total']); ?> 条</span>
		    </div>
		    <div class="pagination-box" data-toggle="pagination" data-total="<?php echo ($show['total']); ?>" data-page-size="<?php echo ($show['listrow']); ?>" data-page-current="1">
		    </div>
	<?php elseif(save == page and $show == null): ?>
		<ul>
			<li><button type="button" class="btn-close" data-icon="close">关闭</button></li>
		</ul>
	<?php else: ?>
		<ul>
			<li><button type="button" class="btn-close" data-icon="close">关闭</button></li>
		</ul><?php endif; ?>

</div>